package cn.itsource.service.impl;

import cn.itsource.constants.ErrorCode;
import cn.itsource.openfeign.LoginOpenFeign;
import cn.itsource.pojo.domain.Customer;
import cn.itsource.mapper.CustomerMapper;
import cn.itsource.pojo.domain.Login;
import cn.itsource.pojo.dto.CustomerRegisterDto;
import cn.itsource.pojo.enumConst.LevelEnum;
import cn.itsource.result.JSONResult;
import cn.itsource.service.ICustomerService;
import cn.itsource.template.WechatTemplate;
import cn.itsource.utils.BitStatesUtils;
import cn.itsource.utils.IdUtil;
import cn.itsource.utils.NameUtil;
import cn.itsource.utils.RConst;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author tt
 * @since 2024-09-04
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private WechatTemplate wechatTemplate;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private LoginOpenFeign loginOpenFeign;


    @Override
    public JSONResult registerOpenId(CustomerRegisterDto customerRegisterDto) {
        String openId = wechatTemplate.getOpenId(customerRegisterDto.getCode());
        //String phoneNumber = wechatTemplate.getPhoneNumber(customerRegisterDto.getPhoneCode());
        Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, openId));
        if(customer==null){
            Customer customer1 = new Customer();
            customer1.setId(IdUtil.getSnowFlackId());
            customer1.setSex(RConst.Num.INT_0);
            customer1.setPhone("15486215845");
            customer1.setEmail("215842551@qq.com");
            customer1.setCreateTime(new Date());
            customer1.setName(NameUtil.getName());
            customer1.setOpenId(openId);
            customer1.setBitState(BitStatesUtils.OP_PHONE);
            customer1.setLevel(LevelEnum.BRONZE.getKey());
            customerMapper.insert(customer1);
            Login login = getLogin(openId, customer1);
            loginOpenFeign.create(login);
            return JSONResult.success();
        }
        return JSONResult.error(ErrorCode.PARAM_PHONE_EXIST);
    }

    private static Login getLogin(String openId, Customer customer1) {
        Login login = new Login();
        login.setId(customer1.getId());
        login.setUsername(customer1.getName());
        login.setPassword("123456");
        login.setType(RConst.Num.INT_2);
        login.setPhone(customer1.getPhone());
        login.setAdmin(RConst.Num.INT_0);
        login.setNickName(login.getUsername());
        login.setOpenId(openId);
        login.setName(customer1.getName());
        //login.setPhone(customer1.getPhone());
        login.setCreateTime(new Date());
        return login;
    }

}
